package cn.com.lyj6851.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;

import cn.com.lyj6851.common.authMobile.SmsCodeAuthenticationSecurityConfig;
import cn.com.lyj6851.common.config.FormAuthenticationConfig;
import cn.com.lyj6851.common.validate.config.ImageValidateCodeSecurityConfig;

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true) 
public class AuthSecurityConfig extends WebSecurityConfigurerAdapter {

	@Autowired
    private FormAuthenticationConfig formAuthenticationConfig;
	
	@Autowired
    private ImageValidateCodeSecurityConfig imageValidateCodeSecurityConfig;
	@Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;
	
	
    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PasswordEncoder passwordEncoder(){ 
        return PasswordEncoderFactories.createDelegatingPasswordEncoder();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
             .passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
    	// 表单登录配置项
        formAuthenticationConfig.configure(http);
        
    	http.apply(imageValidateCodeSecurityConfig)// 应用图形验证码安全配置
    	    .and()
    	    .apply(smsCodeAuthenticationSecurityConfig) // 应用短信验证码认证安全配置
            .and()
	        .authorizeRequests()
	        .antMatchers("/authentication/require",
	                "/authentication/form",
	                "/login",
	                "/code/*",
	                "/**/*.js",
                    "/**/*.css",
                    "/**/*.jpg",
                    "/**/*.png",
                    "/**/*.woff2"
	        		).permitAll()
	        .anyRequest().authenticated()
	        .and()
	        .csrf().disable();
    	
    	//http.httpBasic();
    }
    
 /**
    @Autowired
    private OAuth2WebSecurityExpressionHandler expressionHandler;
    
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/v2/api-docs","/uaa/**").permitAll();

        ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry registry = http.authorizeRequests();
        for (String au:AUTH_WHITELIST) {
            http.authorizeRequests().antMatchers(au).permitAll();
        }
        http.authorizeRequests().anyRequest().authenticated();
        registry.anyRequest()
                .access("@permissionService.hasPermission(request,authentication)");
    }
    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.expressionHandler(expressionHandler);
    }

    @Bean
    public OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler(ApplicationContext applicationContext) {
        OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
        expressionHandler.setApplicationContext(applicationContext);
        return expressionHandler;
    }

     */
    
}